package cn.clark.game.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    private GamePanel gamePanel;
    private JButton startPauseBtn;

    public MainFrame() {
        initUI();
    }

    private void initUI() {
        setTitle("Snake Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        // 游戏面板
        gamePanel = new GamePanel();
        add(gamePanel, BorderLayout.CENTER);

        // 控制面板
        JPanel controlPanel = new JPanel(new FlowLayout());
        startPauseBtn = createButton("开始游戏", e -> toggleGameState());
        JButton restartBtn = createButton("重新开始", e -> gamePanel.resetGame());

        controlPanel.add(startPauseBtn);
        controlPanel.add(restartBtn);
        add(controlPanel, BorderLayout.SOUTH);

        // 菜单栏
        initMenuBar();

        pack();
        setLocationRelativeTo(null);
    }

    // 修正1：使用 ActionListener 代替 EventHandler
    private JButton createButton(String text, ActionListener listener) {
        JButton btn = new JButton(text);
        btn.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btn.addActionListener(listener);
        return btn;
    }

    private void initMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        // 速度菜单
        JMenu speedMenu = new JMenu("速度设置");
        String[] speeds = {"慢速 (300ms)", "中速 (150ms)", "快速 (80ms)", "自定义"};
        for (String speed : speeds) {
            JMenuItem item = new JMenuItem(speed);
            item.addActionListener(e -> handleSpeed(speed));
            speedMenu.add(item);
        }

        menuBar.add(speedMenu);
        setJMenuBar(menuBar);
    }

    private void toggleGameState() {
        if (gamePanel.isRunning()) {
            gamePanel.pauseGame();
            startPauseBtn.setText("继续游戏");
        } else {
            gamePanel.startGame();
            startPauseBtn.setText("暂停游戏");
        }
    }

    private void handleSpeed(String speed) {
        switch (speed) {
            case "慢速 (300ms)":
                gamePanel.setGameSpeed(300);
                break;
            case "中速 (150ms)":
                gamePanel.setGameSpeed(150);
                break;
            case "快速 (80ms)":
                gamePanel.setGameSpeed(80);
                break;
            case "自定义":
                setCustomSpeed();
                break;
        }
    }

    private void setCustomSpeed() {
        String input = JOptionPane.showInputDialog(this, "输入速度 (毫秒):", "自定义速度", JOptionPane.QUESTION_MESSAGE);
        try {
            int speed = Integer.parseInt(input);
            if (speed > 0 && speed <= 1000) {
                gamePanel.setGameSpeed(speed);
            } else {
                showErrorDialog("速度值必须为1-1000之间的整数");
            }
        } catch (NumberFormatException e) {
            showErrorDialog("请输入有效数字");
        }
    }

    private void showErrorDialog(String message) {
        JOptionPane.showMessageDialog(this, message, "输入错误", JOptionPane.ERROR_MESSAGE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
}